<?php

class MenuitemController extends MyController
{	
	function init()
	{
		parent::init();
	}
	
	function actionIndex()
	{	
		$this->redirect(array($this->controller_id.'/view'));
	}
	
	function actionView($menu_id = 0, $language_id = 0)
	{
		$rows = NULL;
	
		if ($menu_id == 0 && $language_id == 0)
		{
			$parent = menuitem::model()->findAll(array(
				"condition" => "parent_id = 0",
				"order" => "menu_id, `language_id`, `order`, id ASC"
				//"order" => " id ASC"
			));
			
			foreach($parent as $p)
			{
				$rows[] = $p;
				$child = menuitem::model()->findAll(array(
					"condition" => "parent_id = " . $p->id,
					"order" => "`language_id`,`order`, id ASC"
				));
				
				foreach($child as $c)
					$rows[] = $c;
			}
		}
		else if ($menu_id == 0 && $language_id != 0)
		{
			$parent = menuitem::model()->findAll(array(
				"condition" => "parent_id = 0 AND language_id = " . $language_id,
				"order" => "menu_id, `language_id`, `order`"
			));
			
			foreach($parent as $p)
			{
				$rows[] = $p;
				$child = menuitem::model()->findAll(array(
					"condition" => "parent_id = " . $p->id,
					"order" => "`language_id`, `order`"
				));
				
				foreach($child as $c)
					$rows[] = $c;
			}
		}
		else if ($menu_id != 0 && $language_id == 0)
		{
			$parent =  menuitem::model()->findAll(array(
				"condition" => "parent_id = 0 AND menu_id = " . $menu_id,
				"order" => "menu_id, `language_id`, `order`, id ASC"
			));
			
			foreach($parent as $p)
			{
				$rows[] = $p;
				$child = menuitem::model()->findAll(array(
					"condition" => "parent_id = " . $p->id,
					"order" => "`language_id`, `order`, id ASC"
				));
				
				foreach($child as $c)
					$rows[] = $c;
			}
			
		}
		else
		{
			$parent = menuitem::model()->findAll(array(
				"condition" => "parent_id = 0 AND menu_id = " . $menu_id . 
					" AND language_id = " . $language_id,
				"order" => "menu_id,`language_id`, `order`"
			));
			
			foreach($parent as $p)
			{
				$rows[] = $p;
				$child = menuitem::model()->findAll(array(
					"condition" => "parent_id = " . $p->id,
					"order" => "`language_id`, `order`"
				));
				foreach($child as $c)
					$rows[] = $c;
			}
		}

		$this->data['rows']= $rows;		
		$this->data["menu_id"] = $menu_id;
		$this->data["language_id"] = $language_id;
		
		$this->render('view', $this->data);
	}
	
	function actionAdd($menu_id = 0, $language_id = 0)
	{	
		$this->action = "Thêm mới";
		$row = new menuitem;
		$row->id = 0;
		$row->menu_id = $menu_id;
		$row->language_id = $language_id;
		
		$this->data["row"] = $row;
		$this->data["menu_id"] = $menu_id;
		$this->data["language_id"] = $language_id;	
		$this->render("form_edit", $this->data);
	}
	
	function actionEdit($menuitem_id = 0, $menu_id = 0, $language_id = 0)
	{
		$this->action = "Cập nhật";
		$menuitem = NULL;
		
		if ($menuitem_id == 0)
		{
			$this->redirect(array($this->controller_id.'/view', "group"=>$menu_id));
		}else{	
		
			$row = menuitem::model()->findByPk($menuitem_id);
			$this->data["row"] = $row;
			$this->data["menu_id"] = $menu_id;
			$this->data["language_id"] = $language_id;
			$this->render("form_edit", $this->data);
		}
	}
	
	function actionSave($menuitem_id = 0, $menu_id = 0, $language_id = 0)
	{
		if ($menuitem_id == 0)
		{
			$row = new menuitem;
			$row->setAttributes($_POST, false);
			if (strlen($row->name))
			{
				$max_order = Yii::app()->db->createCommand(
					"SELECT max(`order`)
					FROM menuitem
					WHERE menu_id = " . $menu_id
				)->queryScalar();
				$row->order = $max_order + 1;
				$row->save();
			}
		}
		else
		{
			$row = menuitem::model()->findByPk($menuitem_id);
			$row->setAttributes($_POST, false);
			if (strlen($row->name) && $row->parent_id != $row->id){
				$row->alias = strtolower(trim(mii::clear_sign($row->name)));
				$row->save();
			}
		}
		
		$this->redirect(array($this->controller_id."/view", 
			"menu_id"=>$menu_id, "language_id"=>$language_id ));
	}
	
	function actionSavenew($menuitem_id = 0, $menu_id = 0, $language_id = 0)
	{
		if ($menuitem_id == 0)
		{
			$row = new menuitem;
			$row->setAttributes($_POST, false);
			if (strlen($row->name))
			{
				$max_order = Yii::app()->db->createCommand(
					"SELECT max(`order`)
					FROM menuitem
					WHERE menu_id = " . $menu_id
				)->queryScalar();
				$row->order = $max_order + 1;
				$row->save();
			}
		}
		else
		{
			$row = menuitem::model()->findByPk($menuitem_id);
			$row->setAttributes($_POST, false);
			if (strlen($row->name) && $row->parent_id != $row->id){
				$row->alias = strtolower(trim(mii::clear_sign($row->name)));
				$row->save();
			}
		}
		
		$this->redirect(array($this->controller_id."/add", 
			"menu_id"=>$menu_id, "language_id"=>$language_id ));
	}
	
	function actionApply($menuitem_id = 0, $menu_id = 0, $language_id = 0)
	{
		if ($menuitem_id == 0)
		{
			$row = new menuitem;
			$row->setAttributes($_POST, false);
			if (strlen($row->name))
			{
				$max_order = Yii::app()->db->createCommand(
					"SELECT max(`order`)
					FROM menuitem
					WHERE menu_id = " . $menu_id
				)->queryScalar();
				$row->order = $max_order + 1;
				$row->save();
			}
		}
		else
		{
			$row = menuitem::model()->findByPk($menuitem_id);
			$row->setAttributes($_POST, false);
			if (strlen($row->name) && $row->parent_id != $row->id){
				$row->alias = strtolower(trim(mii::clear_sign($row->name)));
				$row->save();
			}
		}
		
		$this->redirect(array($this->controller_id."/edit", "menuitem_id" => $menuitem_id,
			"menu_id"=>$menu_id, "language_id"=>$language_id ));
	}
	
	function actionEnable($menuitem_id = 0, $menu_id = 0, $language_id = 0)
	{
		$menuitem = menuitem::model()->findByPk($menuitem_id);
		if (! is_null($menuitem))
		{
			$menuitem->enable = 1 - $menuitem->enable;
			$menuitem->save();	
		}
		$this->redirect(array($this->controller_id."/view", 
			"menu_id"=>$menu_id, "language_id"=>$language_id ));
	}
	
	function actionDefault($menuitem_id = 0, $menu_id = 0, $language_id = 0)
	{
		$menuitem = menuitem::model()->findByPk($menuitem_id);
		$language_id = $menuitem->language_id;
		
		menuitem::model()->updateAll(
			array("default"=>0),
			array("condition"=>"language_id = '" . $language_id . "'")
		);

		$menuitem->default = 1;
		$menuitem->save();
		$this->redirect(array($this->controller_id.'/view', 
			"menu_id"=>$menu_id, "language_id"=>$language_id));
	}
	
	function actionSet_order($menu_id = 0, $language_id = 0)
	{
		//Tao thu tu mac dinh
		$rows = menuitem::model()->findAll();
		foreach($rows as $row)
		{
			$row->order = $row->id;
			$row->save();
		}
		$this->redirect(array($this->controller_id.'/view', 
			"menu_id"=>$menu_id, "language_id"=>$language_id));	
	}
	
	function actionSave_order($menu_id = 0, $language_id = 0)
	{
		$orders = Yii::app()->getRequest()->getPost("order");
		
		foreach($orders as $id=>$value)
		{
			$row = menuitem::model()->findByPk($id);
			$row->order = $value;
			$row->save();
		}
		
		//print_r($orders);
		$this->redirect(array($this->controller_id.'/view', 
			"menu_id"=>$menu_id, "language_id"=>$language_id));		
	}
	
	function actionDelete($menuitem_id, $menu_id = 0, $language_id = 0)
	{
		$row = menuitem::model()->findByPk($menuitem_id);
		menuitem::model()->deleteAll(array("condition" => "parent_id = " . $row->id));
		$row->delete();
		
		$this->redirect(array($this->controller_id.'/view',
			"menu_id"=>$menu_id, "language_id"=> $language_id));
	}
	
	function actionDelete_multi($menu_id = 0, $language_id = 0)
	{
		//var_dump(Yii::app()->getRequest()->getPost("cib"));
		
		$id_array = Yii::app()->getRequest()->getPost("cib");
		
		if (!is_null($id_array))
		{			
			foreach($id_array as $id=>$value)
			{
				$row = menuitem::model()->findByPk($id);
				if (!is_null($row))
				{
					menuitem::model()->deleteAll(array("condition"=>"parent_id = " . $row->id));
					$row->delete();	
				}
			}
		}
		
		$this->redirect(array($this->controller_id.'/view',
			"menu_id"=>$menu_id, "language_id"=> $language_id));
	}
	
	function actionList_parent_id($menu_id)
	{	
		$rows = menuitem::model()->findAll(array(
			"condition" => "enable = 1 AND parent_id = 0 AND menu_id = " . $menu_id,
			"order" => "`order` ASC"
		));
		$this->data["rows"] = $rows;
		
		$this->renderPartial("list_parent_id", $this->data);	
	}
	
	function actionMenuitemtype($menuitem_id = 0, $menu_id = 0, $language_id = 0)
	{
		$row = menuitem::model()->findByPk($menuitem_id);
		$this->data["row"] = $row;
		$this->data["menu_id"] = $menu_id;
		$this->data["language_id"] = $language_id;
		$this->render("menuitemtype", $this->data);
		
	}
}